"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserDao = void 0;
const LogServerHelper_1 = require("../../log_server/LogServerHelper");
const DaoBase_1 = require("../../src/engine/dao/DaoBase");
const Logger_1 = require("../../src/engine/utils/Logger");
const MysqlManager_1 = require("../../src/engine/utils/MysqlManager");
const UserEntity_1 = require("../entity/UserEntity");
const AccountDao_1 = require("./AccountDao");
class UserDao extends DaoBase_1.DaoBase {
    static getRepository() {
        return __awaiter(this, void 0, void 0, function* () {
            let repository = yield this.getRepositoryBase(UserEntity_1.UserEntity, this.getGameDbName());
            return repository;
        });
    }
    static getUserNum() {
        return __awaiter(this, void 0, void 0, function* () {
            let repository = yield this.getRepository();
            let count = yield repository.count();
            return count;
        });
    }
    static is_user_exist_by_userid(userid) {
        return __awaiter(this, void 0, void 0, function* () {
            let repository = yield this.getRepository();
            let result = yield repository.findOneBy({
                userid: userid
            });
            if (result) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static update_user(entity) {
        return __awaiter(this, void 0, void 0, function* () {
            let repository = yield this.getRepository();
            let result;
            if (!repository.hasId(entity)) {
                result = yield repository.save(entity);
            }
            else {
                let updateResult = yield repository.update({ userid: entity.userid }, entity);
                // { generatedMaps: [], raw: [], affected: 1 }
                if (updateResult && updateResult.affected > 0) {
                    result = entity;
                }
            }
            return result;
        });
    }
    static get_user_data_by_userid(userid) {
        return __awaiter(this, void 0, void 0, function* () {
            let repository = yield this.getRepository();
            let result = yield repository.findOneBy({
                userid: userid
            });
            return result;
        });
    }
    static get_user_data_by_account(account) {
        return __awaiter(this, void 0, void 0, function* () {
            let repository = yield this.getRepository();
            let result = yield repository.findOneBy({
                account: account
            });
            Logger_1.Logger.log("get_user_data_by_account", result);
            return result;
        });
    }
    static get_userlistinfo(useridList) {
        return __awaiter(this, void 0, void 0, function* () {
            if (useridList.length > 0) {
                let repository = yield this.getRepository();
                let sql = "select * from  t_users where `userid`=" + useridList[0];
                for (let i = 1; i < useridList.length; i++) {
                    sql += " or `userid`=" + useridList[i];
                }
                let results = yield repository.query(sql);
                if (results && results.length) {
                    return results;
                }
            }
            return [];
        });
    }
    static setRoom_id(userid, room_id) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let sql = "update " + this.getGameDbName() + "." + this.Tb_Name + " set `room_id`=?  where `userid`=?";
            let sqlParams = [room_id, userid];
            let results = yield client.queryPromise(sql, sqlParams);
            if (results && results["affectedRows"] > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static getGems(userid) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let sql = "select gems from t_users WHERE userid = ?";
            let sqlParams = [userid];
            let results = yield client.queryPromise(sql, sqlParams);
            Logger_1.Logger.log("getGems==", results);
            if (results && results.length) {
                return results[0]["gems"];
            }
        });
    }
    static decGems(userid, decNum) {
        return __awaiter(this, void 0, void 0, function* () {
            if (decNum > 0) {
                let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
                let gems = yield this.getGems(userid);
                gems -= decNum;
                if (gems < 0) {
                    gems = 0;
                }
                let sql = 'UPDATE t_users SET gems =  ?' + ' WHERE userid = ?';
                let sqlParams = [gems, userid];
                let results = yield client.queryPromise(sql, sqlParams);
                if (results && results["affectedRows"] > 0) {
                    // LogServerHelper.log_add_user_spend_daily(userid, "gems", decNum)
                    return gems;
                }
                else {
                    return gems;
                }
            }
        });
    }
    static updateGems(userid, gems) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let sql = 'UPDATE t_users SET gems =  ?' + ' WHERE userid = ?';
            let sqlParams = [gems, userid];
            let results = yield client.queryPromise(sql, sqlParams);
            if (results && results["affectedRows"] > 0) {
                return gems;
            }
            else {
                return gems;
            }
        });
    }
    static addGems(userid, addNum) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let gems = yield this.getGems(userid);
            gems += addNum;
            if (gems < 0) {
                gems = 0;
            }
            let sql = 'UPDATE t_users SET gems =  ?' + ' WHERE userid = ?';
            let sqlParams = [gems, userid];
            let results = yield client.queryPromise(sql, sqlParams);
            if (results && results["affectedRows"] > 0) {
                return gems;
            }
            else {
                return gems;
            }
        });
    }
    static addCoins(userid, addNum) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let coins = yield this.getCoins(userid);
            coins += addNum;
            if (coins < 0) {
                coins = 0;
            }
            let sql = 'UPDATE t_users SET coins =  ?' + ' WHERE userid = ?';
            let sqlParams = [coins, userid];
            let results = yield client.queryPromise(sql, sqlParams);
            if (results && results["affectedRows"] > 0) {
                return coins;
            }
            else {
                return coins;
            }
        });
    }
    static decCoins(userid, decNum) {
        return __awaiter(this, void 0, void 0, function* () {
            if (decNum > 0) {
                let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
                let coins = yield this.getCoins(userid);
                coins -= decNum;
                if (coins < 0) {
                    coins = 0;
                }
                let sql = 'UPDATE t_users SET coins =  ?' + ' WHERE userid = ?';
                let sqlParams = [coins, userid];
                let results = yield client.queryPromise(sql, sqlParams);
                if (results && results["affectedRows"] > 0) {
                    LogServerHelper_1.LogServerHelper.log_add_user_spend_daily(userid, "coins", decNum);
                    return coins;
                }
                else {
                    return coins;
                }
            }
        });
    }
    static addMeili(userid, addNum) {
        return __awaiter(this, void 0, void 0, function* () {
            // if (addNum > 0) {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let meilis = yield this.getMeili(userid);
            meilis += addNum;
            if (meilis < 0) {
                meilis = 0;
            }
            let sql = 'UPDATE t_users SET meilis = ?' + ' WHERE userid = ?';
            let sqlParams = [meilis, userid];
            let results = yield client.queryPromise(sql, sqlParams);
            if (results && results["affectedRows"] > 0) {
                return meilis;
            }
            else {
                return meilis;
            }
            // }
        });
    }
    static decMeili(userid, decNum) {
        return __awaiter(this, void 0, void 0, function* () {
            if (decNum > 0) {
                let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
                let meilis = yield this.getMeili(userid);
                meilis -= decNum;
                if (meilis < 0) {
                    meilis = 0;
                }
                let sql = 'UPDATE t_users SET meilis =  ?' + ' WHERE userid = ?';
                let sqlParams = [meilis, userid];
                let results = yield client.queryPromise(sql, sqlParams);
                if (results && results["affectedRows"] > 0) {
                    return meilis;
                }
                else {
                    return meilis;
                }
            }
        });
    }
    static getMeili(userid) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let sql = "select meilis from t_users WHERE userid = ?";
            let sqlParams = [userid];
            let results = yield client.queryPromise(sql, sqlParams);
            Logger_1.Logger.log("getMeili==", results);
            if (results && results.length) {
                return results[0]["meilis"];
            }
        });
    }
    static getCoins(userid) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let sql = "select coins from t_users WHERE userid = ?";
            let sqlParams = [userid];
            let results = yield client.queryPromise(sql, sqlParams);
            Logger_1.Logger.log("getCoins==", results);
            if (results && results.length) {
                return results[0]["coins"];
            }
        });
    }
    static decIncome(userid, decNum) {
        return __awaiter(this, void 0, void 0, function* () {
            if (decNum > 0) {
                let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
                let sql = 'UPDATE t_users SET income = income - ?' + ' WHERE userid = ?';
                let sqlParams = [decNum, userid];
                let results = yield client.queryPromise(sql, sqlParams);
                if (results && results["affectedRows"] > 0) {
                    return true;
                }
                else {
                    return false;
                }
            }
        });
    }
    static getIncome(userid) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let sql = "select income from t_users WHERE userid = ?";
            let sqlParams = [userid];
            let results = yield client.queryPromise(sql, sqlParams);
            if (results && results.length) {
                return results[0]["income"];
            }
        });
    }
    static add_income(userid, addNum) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let income = yield this.getIncome(userid);
            income += addNum;
            if (income < 0) {
                income = 0;
            }
            let sql = 'UPDATE t_users SET income = ?' + ' WHERE userid = ?';
            let sqlParams = [income, userid];
            let results = yield client.queryPromise(sql, sqlParams);
            if (results && results["affectedRows"] > 0) {
                return income;
            }
            else {
                return income;
            }
        });
    }
    static update_income(userid, income) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let sql = 'UPDATE t_users SET income =  ? ' + ' WHERE userid = ?';
            let sqlParams = [income, userid];
            let results = yield client.queryPromise(sql, sqlParams);
            if (results && results["affectedRows"] > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static update_income_coins(userid, income, coins) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let sql = 'UPDATE t_users SET income =  ? , coins=? ' + ' WHERE userid = ?';
            let sqlParams = [income, coins, userid];
            let results = yield client.queryPromise(sql, sqlParams);
            Logger_1.Logger.log("update_income_coins==", sql, sqlParams);
            Logger_1.Logger.log("update_income_coins==2", results);
            if (results && results["affectedRows"] > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static update_meili_coins(userid, meilis, coins) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let sql = 'UPDATE t_users SET meilis =  ? , coins=? ' + ' WHERE userid = ?';
            let sqlParams = [meilis, coins, userid];
            let results = yield client.queryPromise(sql, sqlParams);
            Logger_1.Logger.log("update_meili_coins==", sql, sqlParams);
            Logger_1.Logger.log("update_meili_coins==2", results);
            if (results && results["affectedRows"] > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static saveHeadImg(imgUrl, userid) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let sql = "UPDATE t_users SET headimg = ? WHERE userid = ?";
            let sqlParams = [imgUrl, userid];
            let results = yield client.queryPromise(sql, sqlParams);
            Logger_1.Logger.log("saveHeadImg====", sql, sqlParams, results);
            if (results && results["affectedRows"] > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static setState(userid, state) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let sql = "UPDATE t_users SET state = ? WHERE userid = ?";
            let sqlParams = [state, userid];
            let results = yield client.queryPromise(sql, sqlParams);
            if (results && results["affectedRows"] > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static cancelVip(userid) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let sql = "UPDATE t_users SET lv =0 WHERE userid = ?";
            let sqlParams = [userid];
            let results = yield client.queryPromise(sql, sqlParams);
            if (results && results["affectedRows"] > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static get_main_statistics_info() {
        return __awaiter(this, void 0, void 0, function* () {
            let resultObj = {
                "accountNum": yield AccountDao_1.AccountDao.getAccountNum(),
                "userNum": yield UserDao.getUserNum(),
            };
            return resultObj;
        });
    }
    static get_user_list(pagenum, pagesize) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let start = (pagenum - 1) * pagesize;
            let sql = `select * from t_users  limit ?,?`;
            let sqlParams = [start, pagesize];
            let results = yield client.queryPromise(sql, sqlParams);
            return results;
        });
    }
    static get_user_normal_daili_list(pagenum, pagesize) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let start = (pagenum - 1) * pagesize;
            let sql = `select * from t_users  where lv > 0 limit ?,?`;
            let sqlParams = [start, pagesize];
            let results = yield client.queryPromise(sql, sqlParams);
            return results;
        });
    }
    static get_normal_daili_xiaji_num(invite) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let sql = `select count(userid) as count from t_users where invite = ?`;
            let sqlParams = [invite];
            let results = yield client.queryPromise(sql, sqlParams);
            let count = (results && results.length) ? results[0].count : 0;
            return count;
        });
    }
    static get_user_normal_daili_count() {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let sql = `select count(userid) as count from t_users  where lv > 0`;
            let results = yield client.queryPromise(sql);
            let count = (results && results.length) ? results[0].count : 0;
            return count;
        });
    }
    static get_user_qudao_daili_list(pagenum, pagesize) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let start = (pagenum - 1) * pagesize;
            let sql = `select * from t_users  where qudaodaili > 0 limit ?,?`;
            let sqlParams = [start, pagesize];
            let results = yield client.queryPromise(sql, sqlParams);
            return results;
        });
    }
    static get_user_qudao_daili_count() {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let sql = `select count(userid) as count from t_users  where qudaodaili > 0`;
            let results = yield client.queryPromise(sql);
            let count = (results && results.length) ? results[0].count : 0;
            return count;
        });
    }
    static set_lv(userid, lv) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let sql = "UPDATE t_users SET lv = ? WHERE userid = ?";
            let sqlParams = [lv, userid];
            let results = yield client.queryPromise(sql, sqlParams);
            if (results && results["affectedRows"] > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static clearInvite(invite) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let sql = "UPDATE t_users SET invite = ? WHERE invite = ?";
            let sqlParams = [null, invite];
            let results = yield client.queryPromise(sql, sqlParams);
            if (results && results["affectedRows"] > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static get_lv(userid) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let sql = "select lv from t_users WHERE userid = ?";
            let sqlParams = [userid];
            let results = yield client.queryPromise(sql, sqlParams);
            Logger_1.Logger.log("get_lv==", results);
            if (results && results.length) {
                return results[0]["lv"];
            }
        });
    }
    static set_qudaodaili(userid, qudaodaili) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let sql = "UPDATE t_users SET qudaodaili = ? WHERE userid = ?";
            let sqlParams = [qudaodaili, userid];
            let results = yield client.queryPromise(sql, sqlParams);
            if (results && results["affectedRows"] > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static get_userids_by_invite_and_no_lv(invite) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let sql = "select userid from t_users WHERE invite = ? and lv != 1";
            let sqlParams = [invite];
            let results = yield client.queryPromise(sql, sqlParams);
            Logger_1.Logger.log("get_userids_by_invite_and_no_lv==", results);
            return results;
        });
    }
    static getJindouTop(topNum) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let startIndex = 0;
            let sql = "select *  from t_users   order by coins desc limit ?,?";
            let sqlParams = [startIndex, topNum];
            let rows = yield client.queryPromise(sql, sqlParams);
            Logger_1.Logger.log("getTop=", rows);
            if (rows && rows.length) {
                return rows;
            }
            return [];
        });
    }
}
exports.UserDao = UserDao;
UserDao.Tb_Name = "t_users";
